#include "I2C.h"

sbit I2C_SCL = P2^1;
sbit I2C_SDA = P2^0;

void I2C_Start(void)
{
	I2C_SDA = 1;
	I2C_SCL = 1;
	I2C_SDA = 0;
	I2C_SCL = 0;
}

void I2C_Stop(void)
{
	I2C_SDA = 0;
	I2C_SCL = 1;
	I2C_SDA = 1;
}

void I2C_SendByte(unsigned char Byte)
{
	unsigned char i = 0;
	for (i = 0;i < 8;i++)
	{
		I2C_SDA = Byte & (0x80 >> i);
		I2C_SCL = 1;
		I2C_SCL = 0;
	}
}

unsigned char I2C_ReceByte(void)
{
	unsigned char Byte = 0,i;
	I2C_SDA = 1;
	for (i = 0;i < 8;i++)
	{
		I2C_SCL = 1;
		if (I2C_SDA) Byte |= (0x80 >> i);
		I2C_SCL = 0;
	}
	return Byte;
}
//0为应答，1为非应答
void I2C_SendAck(unsigned char AckBit)
{
	I2C_SDA = AckBit;
	I2C_SCL = 1;
	I2C_SCL = 0;
}

unsigned char I2C_ReceiveAck(void)
{
	unsigned char AckBit = 0;
	I2C_SDA = 1;
	I2C_SCL = 1;
	if (I2C_SDA) AckBit = 1;
	I2C_SCL = 0;
	return AckBit;
}
